#!/usr/bin/env bash

out_success() {
    echo -e "[ \033[32mSuccess\033[0m ] $1"
}

out_warning() {
    echo -e "[ \033[33mWarning\033[0m ] $1"
}

out_error() {
    echo -e "[ \033[35mError\033[0m ] $1"
}

out_info() {
    echo -e "[ \033[36mInfo\033[0m ] $1"
}

{
    _path_config="/data/kuaicdn/bbiz/ksp2p/config/args.conf"
}

{
    # 检查配置文件是否存在
    if [ ! -f "$_path_config" ]; then
        out_error "快手配置不存在 $_path_config"
        exit 1
    fi

    out_info "正在更新快手配置文件 $_path_config"
}

# 更新guid
{
    _guid_old=$(awk -F '=' '/^--guid/{print $NF}' "$_path_config")
    _guid_new=$(echo "$_guid_old" | awk -F ':' '{printf ("%s:%03d", $1,($2+1))}')
    out_info "旧GUID $_guid_old"
    out_info "新GUID $_guid_new"
    sed -i "s/^--guid=.*/--guid=$_guid_new/" "$_path_config"
}

# 清理缓存下的用户配置文件
{
    out_info "清理缓存下的用户配置文件"
    ls /disk/*/cache-ks/user.ini.enc 2>/dev/null | xargs -I@ echo 'rm -rf @' | sh
}

# 重新启动ksp2p容器
{
    out_info "重新部署ksp2p容器"
    docker ps -aqf name=ksp2p | xargs docker rm -f
    sleep 2
    curl -fsSL https://1142.s.kuaicdn.cn:11428/bbiz-ksp2p-roll-t2410240/master/raw/branch/main/boot/install.sh | bash
    sleep 2
    docker exec bbiz-ksp2p-roll-t2410240 bash /apps/gitrce/deploy/release/default/start.sh
    awk 'NF' "$_path_config" | xargs -I@ echo -e "\033[36m@\033[0m"
    out_success "ksp2p重上完成"
}

: <<!
    docker exec bbiz-ksp2p-roll-t2410240 bash /apps/gitrce/deploy/release/t202411140/start.sh
!

